package com.estate.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.ParkingUse;
import com.estate.mapper.ParkingUseMapper;
import com.estate.parking.feign.OwnerFeign;
import com.estate.parking.feign.QuartersFeign;
import com.estate.parking.service.ParkingService;
import com.estate.parking.service.ParkingUseService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ParkingUseServiceImpl extends ServiceImpl<ParkingUseMapper, ParkingUse> implements ParkingUseService {

    @Resource
    private QuartersFeign quartersFeign;

    @Resource
    private OwnerFeign ownerFeign;

    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<ParkingUse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<ParkingUse> list = list(wrapper);
        list.forEach(e -> {
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
            e.setOwner(ownerFeign.find(e.getOwnerId()));
        });
        PageInfo<ParkingUse> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult savePackingUse(ParkingUse parkingUse) {
        parkingUse.setIsDelete(0);
        parkingUse.setUseType(0);
        log.info("新增parkingUse数据:{}", parkingUse);
        boolean b = save(parkingUse);
        if (!b) {
            return ResultTool.fail(501, "新增车位使用失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateParkingUse(ParkingUse parkingUse) {
        log.info("修改parkingUse数据:{}", parkingUse);
        boolean b = updateById(parkingUse);
        if (!b) {
            return ResultTool.fail(503, "修改车位使用失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteParkingUse(String parkingId) {
        log.info("删除parkingUse数据:{}", parkingId);
        boolean b = removeById(parkingId);
        if (!b) {
            return ResultTool.fail(502, "删除车位使用失败");
        }
        return ResultTool.success("success");
    }
}
